//
//  CinModel.swift
//  UUCartoon
//
//  Created by Galaxy on 2024/4/17.
//  Copyright © 2024 qykj. All rights reserved.
//

import UIKit
import Ji
import SwiftyJSON
class CinModel: WebsiteBaseModel,WebsiteProtocol {
    override init() {
        super.init()
        websiteName = "Cin"
        webUrlStr = "https://cin.cx"
        websiteTitleArr = []
        websiteIdArr = []
        type = .cin
    }
    //MARK: 排行列表
    func getCategorySiftList(condition: (category:String,reader:String,progress:String,area:String), pageNum: Int) -> [CartoonModel] {
        []
    }
    //MARK: 获取图片列表
    func getImageList(detailUrl: String) -> [CartoonImgModel] {
        let jiDoc = Ji.init(htmlURL: URL.init(string: detailUrl)!)
        if jiDoc == nil {
            return []
        }else{
            var array:[CartoonImgModel] = []
            // 获取页码
            let pageXpath = "//*[@id='__NEXT_DATA__']/text()"
            let pageNodeArr = jiDoc?.xPath(pageXpath)
            if pageNodeArr!.count > 0 {
                print(pageNodeArr![0])
                let jsonStr = JSON.init(parseJSON: pageNodeArr![0].content!)
                for index in 0...jsonStr["props"]["pageProps"]["data"]["images"]["pages"].count-1{
                    var imgModel = CartoonImgModel.init()
                    let imgUrl = jsonStr["props"]["pageProps"]["data"]["images"]["pages"][index]["t"].stringValue
                    imgModel.imgUrl = imgUrl
                    imgModel.width = CGFloat(jsonStr["props"]["pageProps"]["data"]["images"]["pages"][index]["w"].floatValue)
                    imgModel.height = CGFloat(jsonStr["props"]["pageProps"]["data"]["images"]["pages"][index]["h"].floatValue)
                    imgModel.type = .cin
                    array.append(imgModel)
                }
            }
            return array
        }
    }
    //MARK: 搜索结果
    func getSearchList(keyword: String, pageNum: Int) -> [CartoonModel] {
        []
    }
    //MARK: 搜索推荐
    func getSearchRecommendList() -> [CartoonModel] {
        []
    }
    //MARK: 获取漫画详情
    func getDetailData(urlStr: String) -> CartoonModel {
        CartoonModel.init()
    }
    //MARK: 已完成
    func getDoneList(pageNum: Int) -> [CartoonModel] {
        []
    }
    //MARK: 漫画类型
    func getCategoryList() -> [[CategoryModel]] {
        []
    }
    //MARK: 漫画排行
    func getRankList(pageNum: Int) -> [CartoonModel] {
        []
    }
    //MARK: 最新更新
    func getLatestList(pageNum:Int) -> [CartoonModel] {
        let urlStr = "https://same.yui.pw/api/v6/home/\(pageNum)"
        let semaphore = DispatchSemaphore(value: 0)
        var resultArr: [CartoonModel] = []
        QYRequestData.init().request(urlString: urlStr, params: [:], method: .get) { response in
            let jsonStr = JSON.init(parseJSON: response)
            for index in 0...jsonStr["all"].count-1 {
                let cartoonModel = CartoonModel.init()
                let english = jsonStr["all"][index]["title"]["english"].stringValue
                let japanese = jsonStr["all"][index]["title"]["japanese"].stringValue
                cartoonModel.name = japanese.isEmpty ? english : japanese
                cartoonModel.detailUrl = "\(self.webUrlStr)/v/\(jsonStr["all"][index]["id"].intValue)"
                cartoonModel.author = ""
                cartoonModel.category = ""
                cartoonModel.time = ""
                cartoonModel.type = .cin
                cartoonModel.num = jsonStr["all"][index]["num_pages"].stringValue
                cartoonModel.imgUrl = jsonStr["all"][index]["cover"]["t"].stringValue
                let chapterModel = ChapterModel()
                chapterModel.name = "默认"
                chapterModel.detailUrl = cartoonModel.detailUrl
                cartoonModel.chapterArr = [
                    (name:"默认",data:[chapterModel])
                ]
                resultArr.append(cartoonModel)
            }
            semaphore.signal()
        } failure: { error in
            semaphore.signal()
        }
        semaphore.wait()
        return resultArr
    }
}
